var e, t = getApp(), a = t.globalData.url, r = require("../../../utils/util.js");

Page({
    data: {
        canShare: !1,
        domain: a,
        slider: 5,
        hasNoCoupons: !1,
        selectCouponsId: "",
        selectCouponsPrice: "",
        coupons: [],
        youhuijine: 0,
        curCoupon: null,
        relateOrderId: "",
        timeStart: "",
        yuyueConfirm: !1,
        state: 1,
        content: "",
        noteMaxLen: 140,
        noteNowLen: 0,
        src: "",
        imgArr: [],
        uploadimgArr: [],
        textareaVal: "",
        autoName: "自动洗车"
    },
    onLoad: function(a) {
        if (e = this, this.getOrderInfo(a.id), this.getCoupons(a.id), this.setData({
            serviceOrderId: a.id,
            orderSource: a.orderSource
        }), "1" == a.orderSource) {
            e.setData({
                serviceOrderId: a.relateOrderId,
                relateOrderId: "预约单界面"
            });
            var o = t.globalData.token;
            r.request(t, "wx/cust/getOneCBO", "POST", {
                token: o,
                bespeakOrderId: a.relateOrderId
            }, function(t) {
                e.setData({
                    timeStart: t.data.timeStart,
                    state: t.data.state
                });
            }, function(e) {});
        }
    },
    getOrderInfo: function(e) {
        var a = this;
        r.request(t, "wx/cust/getOneByServiceOrderId", "POST", {
            serviceOrderId: e,
            token: t.globalData.token
        }, function(o) {
            2 == o.data.orderSource && null == o.data.orderRemark && 1e3 == o.data.payAmount && a.setData({
                canShare: !0
            }), a.setData({
                shopId: o.data.shopId,
                url: o.data.url,
                carNo: o.data.carNo,
                serviceOrderId: o.data.serviceOrderId,
                orderState: o.data.orderState,
                shopName: o.data.shopName,
                cuname: o.data.cuname,
                phone: o.data.phone,
                serviceName: o.data.servicePackageName,
                serviceContent: o.data.servicePackageDesc,
                orderStates: o.data.orderStates,
                addTime: o.data.addTime,
                totalAmount: r.convertMath(o.data.totalAmount),
                discountPrice: o.data.discountPrice,
                debateAmount: r.convertMath(o.data.debateAmount),
                buyNum: 1,
                orderTypes: o.data.orderTypes,
                total: r.convertMath(o.data.debateAmount),
                relateOrderId: o.data.relateOrderId,
                orderSource: o.data.orderSource
            }), 6 == o.data.orderState && a.getOrderEvaluate(), null == o.data.servicePackageName && r.request(t, "wx/cust/getOneByAutoWashOrderId", "POST", {
                serviceOrderId: e,
                token: t.globalData.token
            }, function(e) {
                e.state && (null == e.data ? a.setData({
                    autoName: "光速洗车"
                }) : a.setData({
                    autoName: e.data.ruleName
                }));
            }, function(e) {});
        }, function(e) {});
    },
    gobuy: function(e) {
        e.currentTarget.dataset.id, e.currentTarget.dataset.disamount, e.currentTarget.dataset.discount, 
        e.currentTarget.dataset.totalamount;
        wx.navigateTo({
            url: "/pages/service/order-topay/index?id=" + e.currentTarget.dataset.id
        });
    },
    getCoupons: function(e) {
        var a = this, o = t.globalData.token;
        r.request(t, "wx/mkt/getmktCouponByserviceOrderId", "POST", {
            serviceOrderId: e,
            token: o
        }, function(e) {
            var t = e.data;
            t.forEach(function(e) {
                return e.isSeleted = !1;
            }), a.setData({
                coupons: t
            });
        }, function(e) {});
    },
    checkboxChange: function(e) {
        var t = this.data.coupons, a = this;
        t.forEach(function(e) {
            return e.isSeleted = !1;
        });
        var o = e.detail.value[e.detail.value.length - 1];
        if (o >= 0) if (a.data.selectCouponsId == t[Number(o)].couponId) t[Number(o)].isSeleted = !1, 
        a.setData({
            coupons: t,
            selectCouponsId: "",
            selectCouponsPrice: "",
            hasNoCoupons: !1,
            total: a.data.debateAmount
        }); else {
            t[Number(o)].isSeleted = !0, a.setData({
                coupons: t,
                hasNoCoupons: !0,
                selectCouponsId: t[o].couponId,
                selectCouponsPrice: r.convertMath(t[o].deductAmountAmount)
            });
            var n = (a.data.debateAmount - a.data.selectCouponsPrice).toFixed(2);
            a.setData({
                total: n
            });
        }
    },
    wxPay: function(e) {
        var a = this, o = a.data.serviceOrderId, n = a.data.selectCouponsId;
        o ? r.wxpay(t, o, n, "/pages/service/index") : wx.showModal({
            title: "错误",
            content: "无法获取用户资金信息",
            showCancel: !1
        });
    },
    yuyueConfirm: function(e) {
        var a = this, o = t.globalData.token;
        r.request(t, "wx/cust/toShop", "POST", {
            token: o,
            bespeakOrderId: a.data.relateOrderId
        }, function(e) {}, function(e) {});
    },
    wxPayService: function() {
        wx.showToast({
            title: "暂时不可付款",
            icon: "none"
        });
    },
    sliderChange: function(e) {
        var t = "";
        switch (e.detail.value) {
          case 1:
            t = "差评";
            break;

          case 2:
            t = "较差";
            break;

          case 3:
            t = "一般";
            break;

          case 4:
            t = "良好";
            break;

          case 5:
            t = "好评";
            break;

          default:
            t = "差评";
        }
        this.setData({
            slider: e.detail.value,
            evaluate_title: t
        });
    },
    textareaVal: function(e) {
        var t = this, a = e.detail.value, r = parseInt(a.length);
        r > t.data.noteMaxLen || t.setData({
            content: a,
            noteNowLen: r
        });
    },
    addImg: function() {
        e.data.imgArr, e.data.uploadimgArr;
        wx.chooseImage({
            count: 3,
            sizeType: [ "original", "compressed" ],
            sourceType: [ "album", "camera" ],
            success: function(t) {
                var a = t.tempFilePaths;
                e.setData({
                    imgArr: a
                });
            }
        });
    },
    upLoadImge: function(e, a) {
        var r = this;
        wx.uploadFile({
            url: t.globalData.url + "wx/sys/uploadUrl",
            filePath: e,
            name: "file",
            header: {
                "content-type": "multipart/form-data"
            },
            formData: {
                shopId: r.data.shopId,
                evalId: a
            },
            success: function(e) {
                JSON.parse(e.data.trim()).state || wx.showToast({
                    icon: "fail",
                    title: "上传失败",
                    duration: 2e3
                });
            },
            fail: function() {
                wx.showToast({
                    icon: "fail",
                    title: "上传失败",
                    duration: 2e3
                });
            },
            complete: function() {
                wx.hideToast();
            }
        });
    },
    imgDel: function(e) {
        var t = this.data.imgArr, a = e.target.dataset.index;
        if (t.length) for (var r = 0; r < t.length; r++) r == a && t.splice(r, 1);
        this.setData({
            imgArr: t
        });
    },
    previewImage: function(e) {
        wx.previewImage({
            current: e.target.dataset.url,
            urls: [ e.target.dataset.url ]
        });
    },
    report: function() {
        var a = t.globalData.token, o = e.data.content;
        "" != o && null != o || e.setData({
            content: "一次愉快的体验"
        }), r.request(t, "wx/cust/evaluate", "GET", {
            token: a,
            evalScore: e.data.slider,
            evalContent: encodeURIComponent(o),
            orderId: e.data.serviceOrderId
        }, function(t) {
            if (t.state) {
                wx.showToast({
                    title: "评价成功",
                    icon: "success",
                    duration: 3e3
                });
                var a = e.data.imgArr;
                if (null != a) for (var r in a) e.upLoadImge(a[r], t.data.evalId);
                setTimeout(function() {
                    wx.switchTab({
                        url: "/pages/service/index"
                    });
                }, 2e3);
            } else wx.showToast({
                title: "评价失败",
                icon: "cancel"
            });
        }, function(e) {
            wx.showToast({
                title: "评价失败",
                icon: "cancel"
            });
        });
    },
    getOrderEvaluate: function() {
        var e = this, a = t.globalData.token;
        r.request(t, "wx/cust/getEvaluateByOrderId", "GET", {
            orderId: e.data.serviceOrderId,
            token: a
        }, function(t) {
            if (t.state) {
                var a = t.data.evalContent;
                "" == a && (a = "一次愉快的体验"), e.setData({
                    evalScore: t.data.evalScore,
                    evalContent: a,
                    imgArr: t.data.urlList
                });
            }
        }, function(e) {});
    },
    onShareAppMessage: function(t) {
        if ("button" == t.from) return {
            title: "分享红包免费洗车",
            path: "/pages/autoWash/share/index?serviceOrderId=" + e.data.serviceOrderId,
            imageUrl: "https://xc.njrentong.com/back/files/default/autowash/bg5.png",
            success: function(e) {
                wx.showShareMenu({
                    withShareTicket: !0
                });
            },
            fail: function(e) {}
        };
    }
});